<template>
  <div>
    <div style="margin-bottom: 20px;font-size: medium;font-weight: 900;">单位信息</div>
    <!--    输入框-->
    <div class="search">
      <el-form label-width="90px" :inline="true" :model="form" ref="formRef">
        <el-form-item prop="unitName" label="单位名称">
          <el-input v-model="form.unitName" autocomplete="off" placeholder="请输入"></el-input>
        </el-form-item>

        <!--      选择单位类别-->
        <el-form-item label="单位类别" prop="unitCategory">
          <el-input v-model="form.unitCategory" autocomplete="off" placeholder="请输入"></el-input>
          <!-- <el-select v-model="form.unitCategory" placeholder="请选择" style="width: 100%">
            <el-option v-for="item in noticeData" :label="item.unitCategory" :value="item.unitId"></el-option>
          </el-select> -->
        </el-form-item>
        <el-form-item prop="contact" label="联系人">
          <el-input v-model="form.contact" autocomplete="off" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item prop="contactPhone" label="联系方式">
          <el-input v-model="form.contactPhone" autocomplete="off" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item prop="creditCode" label="信用代码">
          <el-input v-model="form.creditCode" autocomplete="off" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item prop="remark" label="备注">
          <el-input v-model="form.remark" autocomplete="off" placeholder="请输入"></el-input>
        </el-form-item>
      </el-form>
    </div>

    <!--    页面下部分区域 企业认证区域-->
    <div class="check">
      <el-checkbox-group v-model="checkList">
        <div style="margin-bottom: 20px;margin-top: 20px;font-size: medium;font-weight: 900;">认证管理
          <el-checkbox label="1" style="margin-left: 30px;">企业认证</el-checkbox>
            <el-checkbox label="2" style="margin-left: 10px;">个人认证</el-checkbox>
        </div>
      </el-checkbox-group>

    <!-- 步骤条区域 -->
    <el-steps :active="1" simple :space="100">
      <el-step title="1.填写信息" icon="el-icon-edit"></el-step>
      <el-step title="2.等待审核" icon="el-icon-upload"></el-step>
      <el-step title="3.审核结果" icon="el-icon-picture"></el-step>
    </el-steps>

    <el-form label-width="86px" :inline="true" style="margin-top: 20px;">
      <el-form-item prop="juridicalPerson" label="法人代表">
        <el-input v-model="form.juridicalPerson" autocomplete="off" placeholder="请输入法人代表"></el-input>
      </el-form-item>
      <el-form-item prop="period" label="营业期限">
        <el-date-picker v-model="form.period" align="right" type="date" placeholder="选择日期" autocomplete="off">
        </el-date-picker>
      </el-form-item>

      <!--   上传凭证-->
      <el-form-item label="上传凭证" prop="image">
        <el-upload name="image"
                   class="avatar-uploader" :action="$baseUrl + '/upload'" :headers="{ token: user.token }"
          list-type="picture" :on-success="handleAvatarSuccess">
          <img v-if="form.image" :src="form.image" class="avatar" :style="{ width: '200px', height: '150px' }">
          <el-button icon="el-icon-upload"></el-button>
        </el-upload>
      </el-form-item>
    </el-form>
    <!--           <el-checkbox-group v-model="checkList">-->
    <!--              <el-checkbox label="3">长期</el-checkbox>-->
    <!--              <div class="label">若无营业期限，请选择长期</div>-->
    <!--          </el-checkbox-group> -->
    <div class="label">请提供清晰、完整，无水印的新版统一社会信用代码的执照原件数码照，复印件需加盖公司红章。（最多上传1张，每张最大10M不能小于25K）</div>
</div>


    <!-- 底部区域 -->
    <span slot="footer">
      <el-button type="primary" style="margin-top: 360px;margin-left:700px" @click="save">保 存</el-button>
      <router-link to="/notice"><el-button style="margin-left:30px">取 消</el-button></router-link>
    </span>

  </div>
</template>

<script>

export default {
  name: 'Add',
  data() {
    return {
      tableData: [],
      checkList: ['1', '选中且禁用', '长期'],
      //当前的页数
      pageNum: 1,
      //当前每页显示多少条数据
      pageSize: 10,
      total: 0,
      form: {
        unitId: this.$route.query.param.unitId,
        unitName: this.$route.query.param.unitName,
        unitCategory: this.$route.query.param.unitCategory,
        creditCode: this.$route.query.param.creditCode,
        contact: this.$route.query.param.contact,
        contactPhone: this.$route.query.param.contactPhone,
        remark: this.$route.query.param.remark,
        juridicalPerson: this.$route.query.param.juridicalPerson,
        period: this.$route.query.param.period,
        image: this.$route.query.param.image,
      },
      user: JSON.parse(localStorage.getItem('xm-user') || '{}'),
      ids: [],
      noticeData: [],
    }
  },
  created() {
    this.load(1)
    this.loadNotice()
  },
  methods: {
    loadNotice() {
      this.$request.get('/units').then(res => {
        if (res.code === '200') {
          this.noticeData = res.data?.rows
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    save() {   // 保存按钮触发的逻辑  它会触发新增或者更新
      this.$refs.formRef.validate((valid) => {
        if (valid) {
          this.$request({
            url: this.form.unitId ? '/units/update' : 'units/insert',
            method: this.form.unitId ? 'PUT' : 'POST',
            data: this.form
          }).then(res => {
            if (res.code === '200') {  // 表示成功保存
              this.$message.success('保存成功')
              this.load(1)
              this.$router.push('/notice')      //跳转回主页面
            } else {
              this.$message.error(res.msg)  // 弹出错误的信息
            }
          })
        }
      })
    },
    load(pageNum) {  // 分页查询
      if (pageNum) this.pageNum = pageNum
      this.$request.get('/units', {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          title: this.title,
        }
      }).then(res => {
        this.tableData = res.data?.rows
        this.total = res.data?.total
      })
    },
    handleSelectionChange(rows) {   // 当前选中的所有的行数据
      this.ids = rows.map(v => v.id)
    },
    handleAvatarSuccess(response) {
      this.form.image = response.data
    },
  },
}
</script>

<style scoped>
.label {
  color: #409EFF;
  font-size: 12px;
}
</style>